package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.DeliveryaddressDao;
import com.foreknow.elm.dao.impl.DeliveryaddressDaoImpl;
import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.service.DeliveryaddressService;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryaddressServiceImpl implements DeliveryaddressService {
    @Override
    public List<Deliveryaddress> slistDeliveryAddressByUserId(String userid) {
        List<Deliveryaddress> list = new ArrayList<>();
        DeliveryaddressDao dao = new DeliveryaddressDaoImpl();
        try {
            list = dao.dlistDeliveryAddressByUserId(userid);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public Deliveryaddress sgetDeliveryAddressById(Integer daid) {
        Deliveryaddress deliveryaddress = new Deliveryaddress();
        DeliveryaddressDao dao = new DeliveryaddressDaoImpl();
        try {
            DBUtil.getConnection();
            deliveryaddress = dao.dgetDeliveryAddressById(daid);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return deliveryaddress;
    }

    @Override
    public int dsaveDeliveryAddress(Deliveryaddress deliveryaddress) {
        int isRight = 0;
        DeliveryaddressDao dao = new DeliveryaddressDaoImpl();
        try {
            DBUtil.beginTransaction();
            isRight = dao.dsaveDeliveryAddress(deliveryaddress);
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();

        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.close();
        }
        return isRight;
    }

    @Override
    public int dupdateDeliveryAddress(Deliveryaddress deliveryaddress) {
        int isRight = 0;
        DeliveryaddressDao dao = new DeliveryaddressDaoImpl();
        try {
            DBUtil.beginTransaction();
            isRight = dao.dupdateDeliveryAddress(deliveryaddress);
            DBUtil.commitTransaction();
        } catch (SQLException e) {

            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.close();
        }

        return isRight;
    }

    @Override
    public int dremoveDeliveryAddress(Integer daid) {
        int isRight = 0;
        DeliveryaddressDao dao = new DeliveryaddressDaoImpl();

        try {
            DBUtil.beginTransaction();
            isRight = dao.dremoveDeliveryAddress(daid);
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.close();
        }
        return isRight;
    }
}
